//
//  YXHomeMeunTypeViewController.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/23.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXHomeMeunTypeViewController.h"
#import "YXPartnerListTableView.h"
#import "YXPartnerHeaderView.h"
#import "YXTradingListView.h"
#import "YXMyMachineView.h"
#import "YXPartnerAleatView.h"
#import "YXMineSettingTypeViewController.h"
#import "YXHomeViewModel.h"
@interface YXHomeMeunTypeViewController ()
@property (nonatomic ,strong) YXPartnerListTableView *ptTableView;
@property (nonatomic ,strong) YXPartnerHeaderView *ptHeaderView;
@property (nonatomic ,strong) YXPartnerHeaderView *tdHeaderView;
@property (nonatomic ,strong) YXTradingListView *tradingListView;
@property (nonatomic ,strong) YXMyMachineView *myMachineView;

@property (nonatomic ,strong) NSString *pageNo;
@property (nonatomic ,assign) BOOL isUp; // YES:上拉 NO:下拉
@property (nonatomic ,strong) NSMutableArray *nameArr;
@end

@implementation YXHomeMeunTypeViewController

- (NSMutableArray *)nameArr {
    if (!_nameArr) {
        _nameArr = [NSMutableArray alloc];
    }
    return _nameArr;
}

- (YXPartnerHeaderView *)ptHeaderView {
    if (!_ptHeaderView) {
        _ptHeaderView = [[YXPartnerHeaderView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 110))];
        _ptHeaderView.type = self.title;
        YXWeakSelf
        [_ptHeaderView setSelectNameBlock:^(NSString *name) {
            
            NSString *str = [[NSString stringWithFormat:@"findMerchantList.do?sessionId=%@&pageNo=%@&pageSize=%@&name=%@", [YXUserInfoManager getUserInfo].sessionId, @1, @10, name] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            
            [BX_NetWorkManager requestForGETWithUrl:str parameter:nil success:^(id responseObject) {
                
                if (REQUESTDATASUCCESS) {
                    weakSelf.nameArr = [YXUserInfoModel mj_objectArrayWithKeyValuesArray:responseObject[@"body"][@"list"]];
                    weakSelf.ptTableView.listArr = weakSelf.nameArr;
//                    NSLog(@"多少：%ld个",weakSelf.nameArr.count);
                }
            } failure:^(NSError *error) {
//                NSLog(@"%@",error);
                [weakSelf showAlertWith:@"维护中，请稍等！"];
            }];

        }];
    }
    return _ptHeaderView;
}

- (YXPartnerHeaderView *)tdHeaderView {
    if (!_tdHeaderView) {
        _tdHeaderView = [[YXPartnerHeaderView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 110))];
        _tdHeaderView.type = self.title;
    }
    return _tdHeaderView;
}


- (YXPartnerListTableView *)ptTableView {
    if (!_ptTableView) {
        _ptTableView = [[YXPartnerListTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) style:(UITableViewStylePlain)];
        _ptTableView.tableHeaderView = self.ptHeaderView;
        
        YXWeakSelf
        // 举报
        [_ptTableView setSelectRowAtIndexPath:^(YXUserInfoModel *model) {
            YXPartnerAleatView *aleatView = [[YXPartnerAleatView alloc] initWithFrame:[UIScreen mainScreen].bounds];
            aleatView.model = model;
            [aleatView showCustomAlertView];
            [aleatView setClickReportBtnBlock:^{
                YXMineSettingTypeViewController *reportVC = [[YXMineSettingTypeViewController alloc] init];
                reportVC.title = @"举报";
                reportVC.typeView = FeedbackView;
                [weakSelf.navigationController pushViewController:reportVC animated:YES];
            }];
            
        }];
        
        // 打电话
        [_ptTableView setSelectPhoneBtnBlock:^(NSString *name, NSString *phone) {
            
            if ([phone isEqualToString:@""]) {
                
            }else {
                NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",phone];
                UIWebView * callWebview = [[UIWebView alloc] init];
                [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
                [weakSelf.view addSubview:callWebview];
            }
          
        }];
        
    }
    return _ptTableView;
}

- (YXTradingListView *)tradingListView {
    if (!_tradingListView) {
        _tradingListView = [[YXTradingListView alloc] initWithFrame:self.view.bounds];
    
    }
    return _tradingListView;
}

- (YXMyMachineView *)myMachineView {
    if (!_myMachineView) {
        _myMachineView = [[YXMyMachineView alloc] initWithFrame:self.view.bounds];
    }
    return _myMachineView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createLeftBarButtonItemWithImage:@"导航栏_002"];
    [self createUI];
}

- (void)createUI {
    
    switch (self.type) {
        case 0:
        {
            self.title = @"合伙人列表";
            self.pageNo = @"1";
            self.isUp = NO;
            [self.view addSubview:self.ptTableView];
            [self requestDataNumber];
            [self requestDataList];
            [self addRefresh];
            [self.ptTableView.mj_header beginRefreshing];

        }
            break;
        case 1:
        {
            self.title = @"交易列表";
            [self.view addSubview:self.tradingListView];
            [self requestDataGetMeAndNextAgents];

        }
            break;
        case 2:
        {
             self.title = @"收益说明";
            UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"收益说明"]];
            image.frame = CGRectMake(10, 0, kScreenWidth-20, kScreenHeight);
            image.contentMode = UIViewContentModeScaleAspectFit;
            image.autoresizesSubviews = YES;
            image.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
            [self.view addSubview:image];
        }
            break;
        case 3:
        {
            self.title = @"我的机具";
            [self.view addSubview:self.myMachineView];
        }
            break;
        default:
            break;
    }
}

// 添加上拉加载和下拉刷新
- (void)addRefresh {
    
    // 添加下拉刷新
    self.ptTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 下拉刷新的时候, 将请求的offset改为0
//        [self.paramDic setObject:@"0" forKey:@"offset"];
        self.pageNo = @"1";
        // 请求数据
        [self requestDataList];
        _isUp = NO; // 标识下拉
    }];
    
    // 添加上拉加载
    self.ptTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self.ptTableView.mj_footer endRefreshing];
        
        // 上拉加载时, 将当前请求的页数加1
//        NSInteger offset = [[self.paramDic objectForKey:@"offset"] integerValue];
        NSInteger pageNo = [self.pageNo integerValue];
        pageNo ++;
//        [self.paramDic setObject:[NSString stringWithFormat:@"%ld",offset] forKey:@"offset"];
        self.pageNo = [NSString stringWithFormat:@"%ld",pageNo];
        // 请求数据
        [self requestDataList];
        _isUp = YES; // 标识下拉
    }];
}

#pragma mark - 请求合伙人列表
- (void)requestDataList {
    YXUserInfoModel *model = [YXUserInfoManager getUserInfo];
    [YXHomeViewModel getQueryFindMerchantListSessionId:model.sessionId PageNo:self.pageNo Name:@"" ModelList:^(NSArray *listArr) {
        self.ptTableView.listArr = listArr;
        if (listArr.count < 10) {
            self.ptTableView.mj_footer.hidden = YES;
        }else {
             self.ptTableView.mj_footer.hidden = NO;
            [self.ptTableView.mj_footer endRefreshing];
        }
        [self.ptTableView.mj_header endRefreshing];
        [self.ptTableView.mj_footer endRefreshing];
    } Failure:^(NSError *error) {
        [self showAlertWithTitle:@"提示" Messge:@"维护中，请稍等！"];
        [self.ptTableView.mj_header endRefreshing];
        [self.ptTableView.mj_footer endRefreshing];
    }];
    
}
#pragma mark - 请求合伙人规模
- (void)requestDataNumber {
    [YXHomeViewModel getQueryMerchantNumberSessionId:[YXUserInfoManager getUserInfo].sessionId ModelList:^(YXPartnerModel *model) {
        self.ptHeaderView.model = model;
    } Failure:^(NSError *error) {
        [self showAlertWithTitle:@"提示" Messge:@"维护中，请稍等！"];
    }];
}

#pragma mark - 查询我和下级代理商的信息ganm
- (void)requestDataGetMeAndNextAgents {
    [SVProgressHUD showWithStatus:@"正在加载中..."];
    [YXHomeViewModel getQueryGetMeAndNextAgentsSessionId:[YXUserInfoManager getUserInfo].sessionId ModelList:^(NSArray *listArr) {
        [SVProgressHUD dismiss];
        self.tradingListView.nameDataArr = listArr;
    } Failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [self showAlertWithTitle:@"提示" Messge:@"维护中，请稍等！"];

    }];
    
}
@end
